﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DNA.Mvc.UI;

namespace DNA.Mvc.Community.Controllers
{
    public class ForumHierarchicalNodeProvider:HierarchicalNodeProviderBase
    {
        private HierarchicalNode root;
        private IForumRepository forumRepository;

        public ForumHierarchicalNodeProvider(IForumRepository repository)
        {
            forumRepository = repository;
        }

        public override IEnumerable<HierarchicalNode> GetChildNodes(HierarchicalNode node)
        {
            int id = int.Parse(node.Key);
            //var forum=GetForum(id);
            var forums = forumRepository.GetForums(id);

            var result = new List<HierarchicalNode>();

            foreach (var forum in forums)
                result.Add(PopulateNode(forum));
            return result;
        }

        public override HierarchicalNode GetParentNode(HierarchicalNode node)
        {
            if (node.Item == null)
                return null;
            var forum = node.Item as Forum;
            var _parent = forumRepository.Find(forum.ParentID);
            // ForumDB.Forums.FirstOrDefault(f => f.ID == forum.ParentID);
            if (_parent == null)
                return null;
            return PopulateNode(_parent);
        }

        public override HierarchicalNode RootNode
        {
            get
            {
                if (root == null)
                {
                    root = new HierarchicalNode(this as IHierarchicalNodeProvider)
                    {
                        Title =GE.Global("forums","Community"),
                        Description = GE.Global("forums", "Community"),
                        NavigateUrl = UrlUtility.CreateUrlHelper().Action("Index", "Forum", new { Area = "Community" }),
                        Key = "0"
                    }; //"Cummunity", "Cummunity", UrlUtlity.CreateUrlHelper().Action("Index", "Forum", new { Area = "Community" }), "0", null, this);
                    root.Attributes.Add("data-key", "0");
                }

                return root;
            }
            set
            {
                root = value;
            }
        }
        protected virtual HierarchicalNode PopulateNode(Forum forum)
        {
            if (forum == null) throw new ArgumentNullException("forum");

            //if (PopulateDelegate != null)
            //    return PopulateDelegate(forum);
            var Url = UrlUtility.CreateUrlHelper();
            string navigateUrl = Url.Action("Threads", "Forum", new { Area = "Community", id = forum.ID });
            if (forum.IsGroup)
                navigateUrl = Url.Action("Group", "Forum", new { Area = "Community", id = forum.ID });

            var node = new HierarchicalNode(this as IHierarchicalNodeProvider)
            {
                Title =GE.GetContent(forum.Title),
                Description =GE.GetContent(forum.Description),
                NavigateUrl = navigateUrl,
                Key = forum.ID.ToString(),
                Item = forum
            };
            //forum.Title, forum.Description, navigateUrl, forum.ID.ToString(), forum, this);
            node.Attributes.Add("data-key", forum.ID.ToString());

            if (forum.IsGroup)
                node.Attributes.Add("data-group", true);
            return node;
        }

        public override HierarchicalNode FindNodeFormKey(string key)
        {
            int id = int.Parse(key);
            return PopulateNode(forumRepository.Find(id));
        }
    }
}
